package cn.com.cifi.mars.util;

import cn.com.cifi.mars.bean.bo.StartAndEndTimeBO;
import cn.com.cifi.mars.bean.vo.report.ProjectReturnCoefficientReportVo;
import cn.com.cifi.mars.bean.vo.report.VsCoefficientReportVo;
import cn.com.cifi.mars.constant.Constants;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 *
 * 类名称：OverallReturnPlanTemplate
 * 类描述：全盘回款计划模板
 * 创建人：jyx
 * 创建时间：2020年01月07日 上午11:20:00
 * @version 1.0.0
 */

@Slf4j
public class ProjectReturnCoefficientTemplate {
    private static final String TEMPLATE_PATH = "/template/project_return_coefficient_template.xlsx";

    private XSSFWorkbook workbook;

    private XSSFSheet sheet;

    //默认sheet索引
    private int defaultSheetIndex = 0;
    //数据开始行
    private int dataBeginRow = 2;
    //当前行标
    private int currentDataRowCursor = dataBeginRow;
    //单元格样式
    private XSSFCellStyle cellStyle;

    //数据行文字单元格样式
    private XSSFCellStyle cellStyleA1;
    //数据行百分比'-'单元格样式
    private XSSFCellStyle cellStyleA2;
    //数据行百分比单元格样式
    private XSSFCellStyle cellStyleA3;

    //文本
    private static final String STRING   = "string ";
    //百分率
    private static final String RATE   = "rate ";

    public ProjectReturnCoefficientTemplate(String name) {
        try {
            InputStream tempInputStream = ProjectReturnCoefficientTemplate.class.getResourceAsStream(TEMPLATE_PATH);
            if (tempInputStream != null) {
                workbook = new XSSFWorkbook(tempInputStream);
                sheet = workbook.getSheetAt(defaultSheetIndex);
                setSheetDate(name);

                cellStyleA1 = sheet.getRow(2).getCell(0).getCellStyle();
                cellStyleA2 = sheet.getRow(2).getCell(1).getCellStyle();
                cellStyleA3 = sheet.getRow(2).getCell(6).getCellStyle();
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new RuntimeException(e);
        }
    }

    public void setSheetDate(String name) {
        workbook.setSheetName(defaultSheetIndex, workbook.getSheetName(defaultSheetIndex).replace("回款计划", name));
    }

    /**
     * 批量插入行
     * @param list 报表数据
     * @param sheetIndex 坐标
     */
    public synchronized void appendRows(List<ProjectReturnCoefficientReportVo> list, int sheetIndex) {
        sheet = workbook.getSheetAt(sheetIndex);
        if(CollectionUtils.isEmpty(list)){
            //删除项目行的数据
            sheet.shiftRows(3, 3, -1);
        }
        this.currentDataRowCursor = this.dataBeginRow;
        for (ProjectReturnCoefficientReportVo vo : list) {
            this.appendRow(vo);
        }
    }

    /**
     * 插入行
     * @param
     */
    public synchronized void appendRow(ProjectReturnCoefficientReportVo vo) {
        XSSFRow row = sheet.getRow(currentDataRowCursor);
        if (row == null) {
            row = sheet.createRow(currentDataRowCursor);
        }
        row.setHeightInPoints(Constants.HEIGHT_POINTS);

        int index = 0;
        //项目信息
        this.appendCell(row, index++,vo.getProjectName(),STRING);
        this.appendCell(row, index++,vo.getProjectCode(),STRING);
        this.appendCell(row, index++,vo.getRegionName(),STRING);
        this.appendCell(row, index++,vo.getCityCompanyName(),STRING);
        this.appendCell(row, index++,vo.getProProductType(),STRING);
        this.appendCell(row, index++,vo.getProProductCode(),STRING);

        List<VsCoefficientReportVo> vsVoList = vo.getVsList();
        Map<Integer, VsCoefficientReportVo> vsVoMap = vsVoList.stream().collect(Collectors.toMap(VsCoefficientReportVo::getNum, s -> s));

        for (int i = 1; i <= 24; i++) {
            VsCoefficientReportVo vsCoefficientReportVo = vsVoMap.get(i);
            this.appendCell(row, index++,vsCoefficientReportVo.getDifference(),RATE);
        }
        for (int i = 1; i <= 24; i++) {
            VsCoefficientReportVo vsCoefficientReportVo = vsVoMap.get(i);
            this.appendCell(row, index++,vsCoefficientReportVo.getCompare(),RATE);
        }
        for (int i = 1; i <= 24; i++) {
            VsCoefficientReportVo vsCoefficientReportVo = vsVoMap.get(i);
            this.appendCell(row, index++,vsCoefficientReportVo.getBeCompared(),RATE);
        }
        currentDataRowCursor++;
    }

    /**
     * 插入列
     * @param row 行
     * @param index 列数
     * @param value 值
     * @param type 样式类型
     */
    private void appendCell(XSSFRow row, int index, Object value, String type) {
        XSSFCell cell = row.getCell(index);
        if (cell == null) {
            cell = row.createCell(index);
        }

        switch (type){
            case STRING:
                cell.setCellStyle(cellStyleA1);
                if(value == null){
                    cell.setCellValue("");
                }else {
                    cell.setCellValue((String) value);
                }
                break;
            case RATE:
                if(value.equals("-")){
                    cell.setCellValue((String)value);
                    cell.setCellStyle(cellStyleA2);
                }else {
                    Double rate = Double.valueOf(value.toString());
                    cell.setCellValue(rate);
                    cell.setCellStyle(cellStyleA3);
                }
                break;
        }
    }

    /**
     * 导出
     *
     * @param outputStream
     * @throws IOException
     */
    public void write(OutputStream outputStream) throws IOException {
        workbook.write(outputStream);
    }
}
